<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">

    <script src="/static/layui/layui.js"></script>
    <script src="/static/js/jquery.min.js"></script>
</head>
<body style="height: 100%">

<form class="layui-form" action="" autocomplete="new-password">

    <div class="layui-form-item" >
        <label class="layui-form-label">UserLoginName</label>
        <div class="layui-input-block" style="padding-left: 10px;padding-right: 10px">
            <input type="text" name="userLoginName"  value=""  lay-verify="required" placeholder="Please enter your login account" autocomplete="new-password" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label">Password</label>
        <div class="layui-input-block" style="padding-left: 10px;padding-right: 10px">
            <input type="password" name="userPwd"    lay-verify="required" placeholder="Please Enter Password" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label">Nickname</label>
        <div class="layui-input-block" style="padding-left: 10px;padding-right: 10px">
            <input type="text" name="userNickName"    lay-verify="required" placeholder="Please Enter nickname" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label" style="height: 137px; line-height: 117px">UserImg</label>
        <input name="userImg" id="userImg"  value="" autocomplete="off" class="layui-input" type="hidden">
        <div class=" layui-upload-drag" id="img" style="position:relative;left:10px">
            <i class="layui-icon">&#xe654;</i>
            <p>Upload</p>
            <img id="demo1"  style="position: absolute;height: 137px;width: 117px;top:0px;left:0px;" />
        </div>
    </div>

    <div class="layui-form-item" >
        <div class="layui-inline" >
            <label class="layui-form-label">Group</label>
            <div class="layui-input-inline">
                <select name="groupId" lay-verify="required">
                    <option value="">Please select group</option>
                    <c:forEach items="${groupList}" var="g">
                        <option value="${g.groupId}">${g.groupName}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="btn" class="layui-btn" lay-submit lay-filter="commit">Submit</button>
            <button type="reset" class="layui-btn layui-btn-primary">Reset</button>
        </div>
    </div>
</form>

<script>
    layui.use(['upload','jquery','form','util'], function(){
        var $ = layui.jquery
            ,upload = layui.upload
            ,form = layui.form;

        //监听提交
        form.on('submit(commit)', function(data){
            var userImg=$("#userImg").val();
            if (userImg==''){
                layer.msg("Please upload pictures");
                return false;
            }

            $.ajax({
                url : '/manage/user/add',
                type : 'post',
                dataType : 'json',
                data : data.field,
                success : function (data) {
                    if (data.code==200){
                        layer.msg(data.msg,{icon: 1, time: 500},function () {
                            layer.close();
                            window.parent.location.reload();
                        });
                    }else{
                        layer.msg(data.msg,{icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#img'
            ,size: 1024*5 //限制文件大小，单位 KB
            ,url: '/uploadFiles/uploadOnlyImage'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').css('display','block').attr('src', result); //链接（base64）
                    $("#btn").attr({"disabled":"disabled"});
                    $("#btn").attr({"class":"layui-btn layui-btn-disabled layui-btn-radius"});
                });
            }
            ,done: function(res){
                //200成功 500失败
                if(res.code == 200){
                    $('#userImg').val(res.data);
                    $("#btn").removeAttr("disabled");
                    $("#btn").attr({"class":"layui-btn"});
                }else{
                    return layer.msg('Upload failed');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">Upload failed</span> <a class="layui-btn layui-btn-xs demo-reload">Retry</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

    });
</script>

</body>
</html>
